package com.xydtech.ecard.merchant.service.impl;

import com.xydtech.common.annotation.DataScope;
import com.xydtech.common.core.domain.entity.SysDept;
import com.xydtech.common.utils.AESEncryptionUtils;
import com.xydtech.common.utils.ShiroUtils;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.merchant.domain.EcardEmployeeStatistics;
import com.xydtech.ecard.merchant.mapper.EcardEmployeeStatisticsMapper;
import com.xydtech.ecard.merchant.service.IEcardEmployeeStatisticsService;
import com.xydtech.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <dd>描述: ~ 员工统计列表信息Service业务层处理</dd>
 * <dd>创建时间：  8:44 2024/3/13</dd>
 * <dd>创建人： yuzhiguo</dd>
 * <dt>版本历史: </dt>
 * Date         Author         Version     Description
 * ------------------------------------------------------------------
 * 2024/3/13     yuzhiguo         1.0        1.0 Version
 */
@Service
public class EcardEmployeeStatisticsServiceImpl implements IEcardEmployeeStatisticsService {

    @Autowired
    private EcardEmployeeStatisticsMapper ecardEmployeeStatisticsMapper;
    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询员工统计列表信息
     * @param ecardEmployeeStatistics
     * @return
     */
    @Override
    @DataScope(deptAlias = "su")
    public List<EcardEmployeeStatistics> selectEmployeeStatistics(EcardEmployeeStatistics ecardEmployeeStatistics, String salt) {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardEmployeeStatistics.getPhoneNumber())) {
                ecardEmployeeStatistics.setPhoneNumber(AESEncryptionUtils.encrypt(ecardEmployeeStatistics.getPhoneNumber(), salt));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        List<EcardEmployeeStatistics> ecardEmployeeStatisticsList = ecardEmployeeStatisticsMapper.selectEmployeeStatistics(ecardEmployeeStatistics);
        SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(ShiroUtils.getSysUser().getDeptId());
        //处理拼接擅长领域和推荐业务
        for (EcardEmployeeStatistics employeeStatistics : ecardEmployeeStatisticsList) {
            try {
                //数据解密（手机号、身份证号）
                if (!StringUtils.objIsNullOrEmpty(employeeStatistics.getPhoneNumber())) {
                    employeeStatistics.setPhoneNumber(AESEncryptionUtils.decrypt(employeeStatistics.getPhoneNumber(), salt));
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            //擅长领域
            String areaExpertise = "";
            //推荐业务
            String recommendation = "";
            Long registId = employeeStatistics.getRegistId();
            //擅长领域处理
            List<EcardEmployeeStatistics> areaExpertiseList = ecardEmployeeStatisticsMapper.selectAreaExpertiseByRegistId(registId, sysDept.getDeptId());
            if (areaExpertiseList.size() > 0){
                for (EcardEmployeeStatistics areaExpertiseStatistics : areaExpertiseList) {
                    if (areaExpertiseStatistics != null){
                        areaExpertise += areaExpertiseStatistics.getAreaExpertise() + ",";
                    }
                }
                if (!StringUtils.objIsNullOrEmpty(areaExpertise)){
                    employeeStatistics.setAreaExpertise(areaExpertise.substring(0, areaExpertise.length() - 1));
                }
            }
            //推荐业务处理
            List<EcardEmployeeStatistics> recommendationList = ecardEmployeeStatisticsMapper.selectRecommendationByRegistId(registId);
            if (recommendationList.size() > 0){
                for (EcardEmployeeStatistics recommendationStatistics : recommendationList) {
                    if (recommendationStatistics != null){
                        recommendation += recommendationStatistics.getRecommendation() + ",";
                    }
                }
                if (!StringUtils.objIsNullOrEmpty(recommendation)){
                    employeeStatistics.setRecommendation(recommendation.substring(0, recommendation.length() - 1));
                }
            }
        }
        return ecardEmployeeStatisticsList;
    }
}
